//
//  XGSettingView.m
//  新闻
//
//  Created by 小果 on 16/3/30.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGSettingView.h"
#import "XGCategoryData.h"
#import "XGCategoryButton.h"

@implementation XGSettingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

#pragma mark - 成员方法
- (void)setDataList:(NSMutableArray *)dataList
{
    _dataList = dataList;
    
    // 1. 删除所有的子视图，避免重复设置时出现问题
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    // 2. 按钮数组
    if (_buttonList == nil) {
        _buttonList = [NSMutableArray array];
    } else {
        [_buttonList removeAllObjects];
    }
    
    // 3. 创建按钮
    [_dataList enumerateObjectsUsingBlock:^(XGCategoryData *data, NSUInteger idx, BOOL *stop) {
        [self createButtonForIndex:idx data:data];
    }];
}

#pragma mark 计算视图大小
- (CGRect)rectForView
{
    // 根据按钮总数，计算出行数，然后增加相应的间距即可
    // 计算视图大小
    NSInteger rows = [self rowsForCount:_dataList.count];
    NSInteger height = rows * kNCButtonHeight + (rows + 1) * kNCButtonEdgeInsetY;
    
    return CGRectMake(0, 0, self.frame.size.width, height);
}

#pragma mark 计算指定索引按钮的frame
- (CGRect)rectForIndexOfButton:(NSInteger)buttonIndex
{
    // 计算每个按钮的宽度
    CGFloat w = (self.bounds.size.width - (kNCButtonCountPerRow + 1) * kNCButtonEdgeInsetX) / kNCButtonCountPerRow;
    
    NSInteger col = [self colForIndex:buttonIndex];
    NSInteger row = [self rowForIndex:buttonIndex];
    CGFloat x = col * w + (col + 1) * kNCButtonEdgeInsetX;
    CGFloat y = row * kNCButtonHeight + (row + 1) * kNCButtonEdgeInsetY;
    
    return CGRectMake(x, y, w, kNCButtonHeight);
}

#pragma mark 创建按钮
- (void)createButtonForIndex:(NSInteger)index data:(XGCategoryData *)data
{
    CGRect rect = [self rectForIndexOfButton:index];
    
    XGCategoryButton *button = [[XGCategoryButton alloc] initWithFrame:rect title:data.categoryName index:index];
    
    // 添加监听方法
    [button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:button];
    [_buttonList addObject:button];
}

#pragma mark 按钮点击方法
- (void)tapButton:(XGCategoryButton *)button
{
    XGCategoryData *data = _dataList[button.index];
    
    NSLog(@"tap button %@", data.categoryName);
}

// 从指定索引删除动画
- (void)removeAnimationFromIndex:(NSInteger)index
{
    [UIView animateWithDuration:0.25 animations:^{
        // 遍历按钮数组中的所有按钮，查找索引数值大于index的按钮
        for (XGCategoryButton *button in _buttonList) {
            if (button.index > index) {
                // 计算index-1对应的按钮位置
                CGRect rect = [self rectForIndexOfButton:(button.index - 1)];
                
                // 将按钮移动到index-1的按钮所在位置
                button.frame = rect;
                
                // 将按钮的index - 1
                button.index--;
            }
        }
    }];
}

#pragma mark - 私有成员方法
#pragma mark 所有行数
- (NSInteger)rowsForCount:(NSInteger)count
{
    return (count + kNCButtonCountPerRow - 1) / kNCButtonCountPerRow;
}

#pragma mark 指定索引所在行数
- (NSInteger)rowForIndex:(NSInteger)index
{
    return (index + kNCButtonCountPerRow) / kNCButtonCountPerRow - 1;
}

#pragma mark 指定索引所在列数
- (NSInteger)colForIndex:(NSInteger)index
{
    return index % kNCButtonCountPerRow;
}


@end
